package com.zzxx.exam.ui;

import com.zzxx.exam.entity.ExamInfo;
import com.zzxx.exam.entity.QuestionInfo;
import com.zzxx.exam.entity.User;
import com.zzxx.exam.service.Controller;
import com.zzxx.exam.service.IdAndPasswordException;
import org.junit.Test;

import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;


public class ClientContext {
    private WelcomeWindow welcomeWindow;
    private LoginFrame loginFrame;
    private MenuFrame menuFrame;
    private ExamFrame examFrame;
    private MsgFrame msgFrame;
    private ResultFrame resultFrame;
    private Controller controller;

    private ExamInfo ei;
    private QuestionInfo qi;



    /**程序一开始显示哪个窗口*/
    public void showView(){
        welcomeWindow.setVisible(true);
        controller.delayswitchView(welcomeWindow,loginFrame);
    }

    /**登录--------切换到主菜单界面*/
    public void login(){
        try {
        /*判断账户密码是否正确*/
            Integer id = loginFrame.getIdField();
            String password = loginFrame.getPwdField();
            User loginUser = null;

            loginUser = controller.login(id, password);
            menuFrame.updateName(loginUser.getName());
            /*切换窗口，登陆界面 -> 主菜单界面*/
            controller.switchView(loginFrame,menuFrame);
        } catch (IdAndPasswordException e) {
            String message = e.getMessage();
            loginFrame.showWarning(message);
        } catch (NumberFormatException e) {
            loginFrame.showWarning("错误！用户名为空！");
        }
    }

    final java.util.Timer timer = new java.util.Timer();
    /**开始考试------切换到考试界面*/
    public void startExam() {
        try {
            ei = controller.satrtExam();
            qi = controller.getFirstQustion();
            examFrame.upDateView(ei, qi);
            controller.switchView(menuFrame, examFrame);


            long start = System.currentTimeMillis();
            //结束时间
            final long end = start + 10 * 60 * 1000;
            //延迟0毫秒（即立即执行）开始，每隔1000毫秒执行一次
            timer.schedule(new TimerTask() {
                public void run() {
                    long now = System.currentTimeMillis();
                    long l = (end - start) - (now - start);
                    long second = (l / 1000) % 60;
                    long min = (l / 60000) % 60;
                    long hour = (l / 600000);
                    examFrame.updateTime(hour, min, second);
                }
            }, 0, 1000);
            timer.schedule(new TimerTask() {
                public void run() {
                    timer.cancel();
                }
            }, new Date(end));
        }catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(menuFrame,e.getMessage());
        }
    }


    /**上一道题 下一道题 交卷*/
    public void next(){
        int index = qi.getQuestionIndex();
        List<Integer> userAnswers = examFrame.getUserAnswers();
        controller.saveUserAnswers(index,userAnswers);
        qi = controller.getQuestion(index + 1);
        examFrame.upDateView(ei,qi);
    }
    public void prev() {
        int index = qi.getQuestionIndex();
        List<Integer> userAnswers = examFrame.getUserAnswers();
        controller.saveUserAnswers(index,userAnswers);
        qi = controller.getQuestion(index - 1);
        examFrame.upDateView(ei,qi);
    }

    public void send() {
        int val = JOptionPane.showConfirmDialog(examFrame, "确认交卷？");
        if (val == JOptionPane.YES_OPTION){
            gameover();
        }
    }
    private int score;
    private void gameover() {
        try {
            List<Integer> finAns = examFrame.getUserAnswers();
            int in = qi.getQuestionIndex();
            controller.saveUserAnswers(in, finAns);
            score = controller.examOver();
            JOptionPane.showMessageDialog(examFrame, "得分为：" + score);
            controller.switchView(examFrame, menuFrame);
            timer.cancel();
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(examFrame,e.getMessage());
        }
    }


    /**显示msg界面*/
    public void showMsg() {
        String rules = controller.showMsg();
        msgFrame.showMsg(rules);
        msgFrame.setVisible(true);
    }

    public void showResult() {
        if (controller.isFinished()){
            String score = Integer.toString(this.score);
            resultFrame.showResult("您的最终成绩为：" + score + "分");
            resultFrame.setVisible(true);
        }else if (controller.isStarted()){
            resultFrame.showResult("正在考试中");
            resultFrame.setVisible(true);
        } else {
            resultFrame.showResult("尚未开始考试");
            resultFrame.setVisible(true);
        }

    }





    /**set注入*/
    public void setLoginFrame(LoginFrame loginFrame) {
        this.loginFrame = loginFrame;
    }

    public void setMenuFrame(MenuFrame menuFrame) {
        this.menuFrame = menuFrame;
    }

    public void setWelcomeWindow(WelcomeWindow welcomeWindow) {
        this.welcomeWindow = welcomeWindow;
    }

    public void setExamFrame(ExamFrame examFrame) {
        this.examFrame = examFrame;
    }

    public void setMsgFrame(MsgFrame msgFrame) {
        this.msgFrame = msgFrame;
    }

    public void setResultFrame(ResultFrame resultFrame) {
        this.resultFrame = resultFrame;
    }

    public void setController(Controller controller) {
        this.controller = controller;
    }



}
